﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:_1906Ultras.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:_1906Ultras.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

namespace _1906Ultras.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:_1906Ultras.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:_1906Ultras.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        private static MainViewModel _main;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}

            CreateMain();
            CreatePhotoViewerViewModel();
            CreateSongViewModel();
            CreateEventViewModel();
        }
        
        
        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearPhotoViewerViewModel();
            ClearSongViewModel();
            ClearEventViewModel();

        }


        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }

        private static PhotoViewerViewModel _photoViewerViewModel;

        /// <summary>
        /// Gets the PhotoViewerViewModel property.
        /// </summary>
        public static PhotoViewerViewModel PhotoViewerViewModelStatic
        {
            get
            {
                if (_photoViewerViewModel == null)
                {
                    CreatePhotoViewerViewModel();
                }

                return _photoViewerViewModel;
            }
        }

        /// <summary>
        /// Gets the PhotoViewerViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public PhotoViewerViewModel PhotoViewerViewModel
        {
            get
            {
                return PhotoViewerViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the PhotoViewerViewModel property.
        /// </summary>
        public static void ClearPhotoViewerViewModel()
        {
            _photoViewerViewModel.Cleanup();
            _photoViewerViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the PhotoViewerViewModel property.
        /// </summary>
        public static void CreatePhotoViewerViewModel()
        {
            if (_photoViewerViewModel == null)
            {
                _photoViewerViewModel = new PhotoViewerViewModel();
            }
        }




        private static SongViewModel _songViewModel;

        /// <summary>
        /// Gets the SongViewModel property.
        /// </summary>
        public static SongViewModel SongViewModelStatic
        {
            get
            {
                if (_songViewModel == null)
                {
                    CreateSongViewModel();
                }

                return _songViewModel;
            }
        }

        /// <summary>
        /// Gets the SongViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SongViewModel SongViewModel
        {
            get
            {
                return SongViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the SongViewModel property.
        /// </summary>
        public static void ClearSongViewModel()
        {
            _songViewModel.Cleanup();
            _songViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the SongViewModel property.
        /// </summary>
        public static void CreateSongViewModel()
        {
            if (_songViewModel == null)
            {
                _songViewModel = new SongViewModel();
            }
        }



        private static EventViewModel _eventViewModel;

        /// <summary>
        /// Gets the EventViewModel property.
        /// </summary>
        public static EventViewModel EventViewModelStatic
        {
            get
            {
                if (_eventViewModel == null)
                {
                    CreateEventViewModel();
                }

                return _eventViewModel;
            }
        }

        /// <summary>
        /// Gets the EventViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public EventViewModel EventViewModel
        {
            get
            {
                return EventViewModelStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the EventViewModel property.
        /// </summary>
        public static void ClearEventViewModel()
        {
            _eventViewModel.Cleanup();
            _eventViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the EventViewModel property.
        /// </summary>
        public static void CreateEventViewModel()
        {
            if (_eventViewModel == null)
            {
                _eventViewModel = new EventViewModel();
            }
        }


       
    }
}